package wili.a;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
//import net.bytebuddy.ByteBuddy;
//import net.bytebuddy.agent.ByteBuddyAgent;
//import net.bytebuddy.dynamic.loading.ClassReloadingStrategy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 *
 */
public class Refs {
	public static int inc;

	public static final Map<Object, List<Object>> dMap = new TreeMap<>();

	static int test3;

	public static boolean useAttsBean() {
		return "asdf".isEmpty();
	}

	static void log(String msg, Object... args) {
		if (++inc == 23)
			pause();
		System.out.println(msg);
		if (msg.contains("dimView:"))// dimView:VOIGNP
			"".isEmpty();
		else if (msg.contains(".listView"))
			"".isEmpty();// eg. OPOR.listView.json, _Template.listView.json
		else if (msg.contains("loadStaticMetaTable:OIGN"))
			"".isEmpty();
	}

	public static boolean att() {
		return true;
	}

	public static boolean misc(String msg, Object... args) {
		return true;
	}

	public static synchronized void key(String msg, Object... args) {
		log(msg, msgs);
	}

	/**
	 * file or cache.
	 */
	public static boolean cache(String msg, Object... args) {// cache
		log(msg, args);
		if (msg.contains("dimView:"))// dimView:VOIGNP
			"".isEmpty();
		else if (msg.contains(".listView"))
			"".isEmpty();// eg. OPOR.listView.json, _Template.listView.json
		else if (msg.contains("loadStaticMetaTable:OIGN"))
			"".isEmpty();
		else if (msg.contains("detailView:"))
			"".isEmpty();
		return true;
	}

	public static void sql(String sql, Object... args) {// CUFD VOIGNP
		if (sql.isEmpty())
			return;
		try {
			System.out.println("wili/sql" + inc + ", " + sql);
			if (sql.contains(".gson"))
				"".isEmpty();
			if ("".isEmpty())
				return;
			msgs.add(sql, args);
			if (sql.contains(""))
				pause();
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}

	public static void pause() {
	}

	public static boolean u2(Object msg) {
		return true;
	}

	public static void main(String[] args) throws SQLException {
		int opt = 1;
		opt = 2;
		if (opt == 1) {
			System.out.println(Object[][][].class.getCanonicalName());
			// System.out.println(int.class.getPackage().getName());
		} else {
			Connection ret = null;
			try {
				ret = connectHana1();
				System.out.println("con=   " + ret);
			} finally {
				if (ret != null)
					ret.close();
			}
		}
	}

	public static Connection connectHana1() {
		String st = "jdbc:sap://10.58.108.224:30013/?databaseName=DC1";
		int opt = -1;
		opt = 1;
		if (opt == 1) {
			st = "jdbc:sap://10.193.81.110:30013/?databaseName=DC1&useSSL=false";
			st = "jdbc:sap://10.58.108.224:30013/?databaseName=DC1";
		} else if (opt == 2) {
		}
		return connectDB(st, "SYSTEM", "Initial0");
	}

	/**
	 * @param url
	 * @param user
	 * @param pwd
	 * @return
	 */
	public static Connection connectDB(String url, String user, String pwd) {
		/*
		 * jdbc:oracle:thin:@vprccnrsolec-02:1521:orcl jdbc:oracle:thin:@127.0.0.1:1521:
		 */
		Connection conn = null;
		try {
			// Class.forName("oracle.jdbc.driver.OracleDriver");
			// com.sap.db.jdbc.Driver.class.getName();
			Class.forName("com.sap.db.jdbc.Driver");// DriverSapDB
			conn = DriverManager.getConnection(url, user, pwd);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return conn;
	}

	public static boolean skipClassObj() {
		// class is network, if A.class referenced B.class, got A's file is not enough,
		// if we deep in to a big project, it is costly.
		return true;
	}

	public static class Msgs {
		public List<String> list = new ArrayList<>();

		int inc = 0;

		public String toString() {
			StringBuilder sb = new StringBuilder(list.size() * 10);
			for (int i = list.size() - 1, j = 0; i >= 0; i--, j++) {
				sb.append("\n " + j + ", " + list.get(i));
			}
			String st = sb.toString();
			log("msg.inc=" + list.size() + ",\n " + st);
			return st;
		}

		public void clear() {
			inc = 0;
			list.clear();
		}

		public void add(String msg, Object... values) {
			synchronized (list) {
				++inc;
				if (list.size() > 2000) {
					List<String> lis = new ArrayList(list.subList(1000, list.size()));
					list.clear();
					list.addAll(lis);
				}
				if (values.length > 0) {
					msg += "; " + Arrays.asList(values);
				}
				list.add(0, msg);
			}
		}
	}

	public static String st(Object st) {// 便于程序自动引用替换。
		return String.valueOf(st);
	}

	public static void print(List list) {
		StringBuilder sb = new StringBuilder();
		for (Object ele : list) {
			sb.append("\n " + ele);
		}
		log(sb.toString());
	}

	static final Msgs msgs = new Msgs();

	public static void clear() {
		msgs.list.clear();
	}

	public static boolean not(String nam) {
		return false;
	}

	// public static void file(String msg){ return }
	public static String concateMapValues(Map ff, String deli, String... keys) {
		StringBuilder sb = null;
		for (int i = 0; i < keys.length; i++) {
			Object st = ff.get(keys[i]);
			if (st instanceof Number || st instanceof String) {
				if (st != null && !st.toString().isEmpty()) {
					if (sb == null)
						sb = new StringBuilder();
					sb.append(st);
					if (i < keys.length)
						sb.append(deli);
				}
			}
		}
		if (sb == null)
			return "";
		return sb.toString();
	}

	public static boolean now() {
		return true;
	}

	public static boolean yes() {
		return true;
	}

	public static void onEvent(int opt, Object... msg) {
		if (opt < 0)
			return;
	}

	public static boolean fm() {
		// 旧值 vs 新值 ~>规则值
		// 旧值 vs 规则值 ~> 新值
		return false;
	}

	public static int r1 = 1, r2 = 1, r3 = 3, r4 = 1, r5 = 1;

	public static String preLog;
}